package com.example.mvp.mvppromote.presenter;

import com.example.mvp.mvppromote.base.BasePresenter;
import com.example.mvp.mvppromote.model.WeiXnBean;
import com.example.mvp.mvppromote.network.Network;
import com.example.mvp.mvppromote.ui.iview.IWeiXinView;
import com.example.mvp.mvppromote.utils.AppManager;
import com.example.mvp.mvppromote.utils.ToaskUtil;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 描述
 * Created by 武彤宇 on 2016/6/27 0027
 */
public class WeiXinPresenter extends BasePresenter<IWeiXinView> {
    private IWeiXinView iWeiXinView;

    public WeiXinPresenter(IWeiXinView iWeiXinView) {
        super(iWeiXinView);
        this.iWeiXinView = iWeiXinView;
    }

    public void getWeiXin(int page) {
        showDialog(iWeiXinView.getMyContext());
        Subscription subscription = Network.getWeiXinApi().getWeixin(page).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<WeiXnBean>() {
                    @Override
                    public void onCompleted() {
                        closeDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(WeiXnBean weiXnBean) {
                        closeDialog();
                        if (weiXnBean.getCode() == 200) {
                            iWeiXinView.updateList(weiXnBean.getNewslist());
                        } else {
                            ToaskUtil.toastShort(AppManager.getAppManager().currentActivity(), "网络请求失败!");
                        }
                    }
                });
        addSubscribe(subscription);

    }

}
